package com.lzw.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.anno.Log;
import com.lzw.pojo.Student;
import com.lzw.service.admin.StudentManagementService;
import com.lzw.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/studentManagement")
@CrossOrigin(origins = "http://localhost:7070")
public class StudentManagementController {

    @Autowired
    private StudentManagementService studentManagementService;

    @Autowired
    private OSSUtil ossUtil;

    //分页查询学生信息
    @GetMapping("/list")
    public PageInfo<Student> listStudents(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name, String gender, String phone){
        PageHelper.startPage(pageNum,pageSize);
        List<Student> list = studentManagementService.listStudents(pageNum, pageSize, name, gender, phone);
        return new PageInfo<>(list);
    }

    //添加学生
    @Log
    @PostMapping("/add")
    public ResponseEntity<String> addStudent(@RequestParam("name") String name,
                             @RequestParam("username") String username,
                             @RequestParam("password") String password,
                             @RequestParam("gender") String gender,
                             @RequestParam("phone") String phone,
                             @RequestParam("email") String email,
                             @RequestParam("dormitoryId") Integer dormitoryId,
                             @RequestParam("buildingId") Integer buildingId,
                             @RequestParam("image") MultipartFile imageFile) {
        try {
            // 上传头像
            String imageUrl = ossUtil.uploadImage(imageFile, "student_images");

            // 创建学生对象
            Student student = new Student();
            student.setName(name);
            student.setUsername(username);
            student.setPassword(password);
            student.setGender(gender);
            student.setPhone(phone);
            student.setEmail(email);
            student.setDormitoryId(dormitoryId);
            student.setBuildingId(buildingId);
            student.setImage(imageUrl);

            // 调用服务添加学生
            studentManagementService.addStudent(student);
            return ResponseEntity.ok("添加学生成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); // 返回具体的错误信息
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("添加学生失败: " + e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //批量删除
    @Log
    @DeleteMapping("/delete/{ids}")
    public String deleteStudent(@PathVariable List<Integer> ids){
        studentManagementService.deleteStudent(ids);
        return "学生删除成功";
    }

    //更新学生信息
    @Log
    @PutMapping("/update")
    public ResponseEntity<String> updateStudent(@RequestParam("studentId") Integer studentId,
                                                @RequestParam(value = "name", required = false) String name,
                                                @RequestParam(value = "username", required = false) String username,
                                                @RequestParam(value = "password", required = false) String password,
                                                @RequestParam(value = "gender", required = false) String gender,
                                                @RequestParam(value = "phone", required = false) String phone,
                                                @RequestParam(value = "email", required = false) String email,
                                                @RequestParam(value = "dormitoryId", required = false) Integer dormitoryId,
                                                @RequestParam(value = "buildingId", required = false) Integer buildingId,
                                                @RequestParam(value = "image", required = false) MultipartFile imageFile) {
        try {
            // 查找旧学生信息
            Student student = studentManagementService.getStudentById(studentId);
            if (student == null) {
                return new ResponseEntity("学生不存在", HttpStatus.NOT_FOUND);
            }

            // 更新字段
            if (name != null) student.setName(name);
            if (username != null) student.setUsername(username);
            if (password != null) student.setPassword(password);
            if (gender != null) student.setGender(gender);
            if (phone != null) student.setPhone(phone);
            if (email != null) student.setEmail(email);
            if (dormitoryId != null) student.setDormitoryId(dormitoryId);
            if (buildingId != null) student.setBuildingId(buildingId);

            // 如果上传了新头像，则更新头像 URL
            if (imageFile != null && !imageFile.isEmpty()) {
                String imageUrl = ossUtil.uploadImage(imageFile, "student_images");
                student.setImage(imageUrl);
            }

            // 更新学生信息
            studentManagementService.updateStudent(student);
            return ResponseEntity.ok("更新学生信息成功");
        } catch (IllegalArgumentException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); // 返回具体的错误信息
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>("更新学生信息失败: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //根据id查询学生信息
    @GetMapping("/get/{id}")
    public Student getStudentById(@PathVariable("id") int id){
        return studentManagementService.getStudentById(id);
    }
}
